package de.baumann.browser.model;

import android.text.TextUtils;

import java.io.Serializable;
import java.util.List;

import de.baumann.browser.api.net.util.OtherHttpUtil;
import de.baumann.browser.api.net.vo.Notice;
import de.baumann.browser.api.net.vo.OdinWithdrawRecord;
import de.baumann.browser.api.net.vo.Result;
import de.baumann.browser.api.net.vo.Weather;
import de.baumann.browser.rx.RxSchedulers;
import de.baumann.browser.utils.ErrorMsg;
import de.baumann.browser.utils.InternalStorageUtil;
import de.baumann.browser.utils.SPUtilKt;
import io.reactivex.Observable;
import io.reactivex.functions.Function;

public class CenterModel extends LicenceModel {

    /**
     * 提现获取验证码
     *
     * @param amount
     * @param ethUrl
     * @param phone
     * @return
     */
    @Deprecated
    public Observable<Result> withdrawGetSmsCode(String amount, String ethUrl, String phone) {
        if (TextUtils.isEmpty(amount)) {
            return Observable.just(new Result(ErrorMsg.WITHDRAW_AMOUNT_EMPTY));
        }

        if (TextUtils.isEmpty(ethUrl)) {
            return Observable.just(new Result("", 1415));
        }

        if (TextUtils.isEmpty(phone)) {
            return Observable.just(new Result("", 102));
        }
        return OtherHttpUtil.getSmsCode(phone, 2,"").compose(RxSchedulers.ioMain());
    }

    public Observable<Result> withdraw(String amount, String smscode) {
        if (TextUtils.isEmpty(amount)) {
            return Observable.just(new Result(ErrorMsg.WITHDRAW_AMOUNT_EMPTY));
        }
        if (TextUtils.isEmpty(smscode)) {
            return Observable.just(new Result(ErrorMsg.SMSCODE_EMPTY));
        }
        return OtherHttpUtil.withdraw(amount, smscode).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<OdinWithdrawRecord>>> getWithdrawRecord() {
        return OtherHttpUtil.getWithdrawRecord().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<Notice>>> getNotice() {
        long cacheTime = SPUtilKt.getLong("noticeCacheTime", 0);
        long currentTime = System.currentTimeMillis();
        if ((currentTime - cacheTime) < TIMEDIFFERENCE) {
            List<Notice> notices = (List<Notice>) InternalStorageUtil.getInstance().getSerializable("notice");
            if (notices != null) {
                return Observable.just(new Result<>("", 1000, notices));
            }
        }

        return OtherHttpUtil.getNotice()
                .map(listResult -> {
                    InternalStorageUtil.getInstance().save("notice", (Serializable) listResult.getData());
                    SPUtilKt.put("noticeCacheTime", System.currentTimeMillis());
                    return listResult;
                })
                .compose(RxSchedulers.ioMain());
    }

    public Observable<Result> noticeRead(String noticeId) {
        return OtherHttpUtil.noticeRead(noticeId).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<Weather>> getWeather(String city) {
        long cacheTime = SPUtilKt.getLong("weatherCacheTime", 0);
        long currentTime = System.currentTimeMillis();
        if ((currentTime - cacheTime) < TIMEDIFFERENCE) {
            Weather weather = (Weather) InternalStorageUtil.getInstance().getSerializable("weather");
            if (weather != null) {
                return Observable.just(new Result<>("", 1000, weather));
            }
        }

        return OtherHttpUtil.getWeather(city)
                .map(weatherResult -> {
                    InternalStorageUtil.getInstance().save("weather", weatherResult.getData());
                    SPUtilKt.put("weatherCacheTime", System.currentTimeMillis());
                    return weatherResult;
                })
                .compose(RxSchedulers.ioMain());
    }

}
